package com.demo.xmtx.controller;

import com.demo.xmtx.entity.Order;
import com.demo.xmtx.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/orders")
@Tag(name = "订单管理")

public class OrderController {

  @Autowired
  private OrderService orderService;

  @Operation(summary = "创建订单")
  @PostMapping
  public ResponseEntity<Order> createOrder(@RequestParam Long userId,
      @RequestParam Long productId,
      @RequestParam Integer quantity) {
    Order order = orderService.createOrder(userId, productId, quantity);
    return ResponseEntity.ok(order);
  }

  @Operation(summary = "查询订单详情")
  @GetMapping("/{id}")
  public ResponseEntity<Order> getOrderById(@PathVariable Long id) {
    Order order = orderService.getOrderById(id);
    return ResponseEntity.ok(order);
  }

  @Operation(summary = "删除订单")
  @DeleteMapping("/{id}")
  public ResponseEntity<Void> deleteOrderById(@PathVariable Long id) {
    orderService.deleteOrderById(id);
    return ResponseEntity.noContent().build();
  }

  @Operation(summary = "更新订单状态为已购买")
  @PutMapping("/{id}/purchase")
  public ResponseEntity<Void> purchaseOrder(@PathVariable Long id) {
    orderService.purchaseOrder(id);
    return ResponseEntity.noContent().build();
  }
}